package com.itlong.cloud.property.service.impl;

import com.itlong.cloud.POJO.DTO.property.PropertyEquipRecordGetDTO;
import com.itlong.cloud.POJO.PO.VisitorPassRecordPO;
import com.itlong.cloud.POJO.VO.property.PropertyEquipOldVisitorRecordGetVO;
import com.itlong.cloud.POJO.VO.property.PropertyEquipRecordGetVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.PropertyEquipTypeEnum;
import com.itlong.cloud.property.dao.IDeviceCloudIntercomRecordDao;
import com.itlong.cloud.property.dao.IDeviceElevatorRecordDao;
import com.itlong.cloud.property.dao.IDeviceEntranceRecordDao;
import com.itlong.cloud.property.dao.IPropertyEquipDao;
import com.itlong.cloud.property.service.IPropertyEquipRecordService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      通行记录实现类
 * </desc>
 *
 * @createDate 2017/10/11
 */
@Service
public class PropertyEquipRecordServiceImpl implements IPropertyEquipRecordService {


    //电梯通行记录Dao
    @Autowired
    IDeviceElevatorRecordDao iDeviceElevatorRecordDao;

    //门禁通行记录Dao
    @Autowired
    IDeviceEntranceRecordDao iDeviceEntranceRecordDao;

    //云对讲通行记录Dao
    @Autowired
    IDeviceCloudIntercomRecordDao iDeviceCloudIntercomRecordDao;

    @Autowired
    IPropertyEquipDao iPropertyEquipDao;
    /**
     * <desc>
     *      分页查询电梯通行记录
     * <desc/>
     * @param   propertyDeviceRecordGetDTO   通行记录查询DTO
     * @return  List<PropertyEquipRecordGetVO>
     * @author Biao.R
     * @createDate 2017/10/10
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<PropertyEquipRecordGetVO> getEquipRecordByPage(PropertyEquipRecordGetDTO propertyDeviceRecordGetDTO) throws Exception {
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(propertyDeviceRecordGetDTO);
        params.put("projectId", propertyDeviceRecordGetDTO.getProjectId());

        Page<PropertyEquipRecordGetVO> page = new Page<>();
        page.getPage().setPageSize(propertyDeviceRecordGetDTO.getPageSize());
        page.getPage().setCurrentPage(propertyDeviceRecordGetDTO.getCurrentPage());
        params.put("currentPage", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());

        List<PropertyEquipRecordGetVO> recordList = new ArrayList<>();
        Integer recordCount = 0;
        //根据不同的设备型号查询通行记录
        Integer deviceType = propertyDeviceRecordGetDTO.getDeviceType();
        switch (PropertyEquipTypeEnum.getByEquipType(deviceType)) {
            case ELEVATOR:
                recordList = iDeviceElevatorRecordDao.getElevatorRecordByPage(params);
                recordCount = iDeviceElevatorRecordDao.getElevatorRecordCount(params);
                break;
            case ENTRANCE:
                recordList = iDeviceEntranceRecordDao.getEntranceRecordByPage(params);
                recordCount = iDeviceEntranceRecordDao.getEntranceRecordCount(params);
                break;
            case CLOUD_INTERCOM:
                recordList = iDeviceCloudIntercomRecordDao.getCloudIntercomRecordByPage(params);
                recordCount = iDeviceCloudIntercomRecordDao.getCloudIntercomRecordCount(params);
                break;
            default:
                return null;
        }
        recordList.forEach(vo->{
            if(StringUtils.isNotBlank(vo.getPhoneNum()) && vo.getPhoneNum().length() >= 11) {
                vo.setPhoneNum(vo.getPhoneNum().substring(0, 3) + "****" + vo.getPhoneNum().substring(7, 11));
            }
        });
        page.setRows(recordList, recordCount);
        return page;
    }

    /**
     * <desc>
     *      查询项目中的云对讲数量
     * <desc/>
     * @param  projectId    待查询的projectId
     * @return 该项目的云对讲通行纪录数量
     * @author Biao.R
     * @createDate 2018/1/12
     */
    @Override
    public Integer countCloudRecordByProjectId(String projectId) {
        return iDeviceCloudIntercomRecordDao.countCloudRecordByProjectId(projectId);
    }

    /**
     * <desc>
     *     将通行记录中开门方式为动态密码和二维码的旧数据迁移到访客通行记录表中
     * <desc/>
     *@param  propertyDeviceRecordGetDTO  分页查询通行记录
     * @return  MessageVO 标准格式化响应结果
     * @author Juguang.S
     * @createDate 2018/08/13
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void getVistitorInfo(PropertyEquipRecordGetDTO propertyDeviceRecordGetDTO) throws Exception {
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(propertyDeviceRecordGetDTO);

        List<PropertyEquipOldVisitorRecordGetVO> recordList = new ArrayList<>();
        List<Map<String, Object>> savepassRecordList = new ArrayList<>(); //
        List<String> deviceUniqueArr = new ArrayList<>();
        recordList = iDeviceElevatorRecordDao.getRecordByVisitor(params);
        for(PropertyEquipOldVisitorRecordGetVO equipRecordGetVO : recordList){
            VisitorPassRecordPO po = new VisitorPassRecordPO();
            po.setProjectId(equipRecordGetVO.getProjectId());
            po.setDeviceUnique(equipRecordGetVO.getDeviceUnique());
            po.setPassTime(equipRecordGetVO.getPassTime());
            po.setPassType(equipRecordGetVO.getOpenType()==10?1:2);
            po.setCreateTime(new Date());
            po.setStatus(1);
            deviceUniqueArr.add(equipRecordGetVO.getDeviceUnique());
            savepassRecordList.add(SqlUtil.durableData(po, PlatformConstants.TABLE_SAVE));
        }
        if(savepassRecordList.size()>0){
            iDeviceElevatorRecordDao.batchSave(savepassRecordList);
        }
        if(deviceUniqueArr.size()>0){
            iDeviceElevatorRecordDao.updateOldVisitor(deviceUniqueArr);
            iDeviceEntranceRecordDao.updateOldVisitor(deviceUniqueArr);
            iDeviceCloudIntercomRecordDao.updateOldVisitor(deviceUniqueArr);
        }

    }

    /**
     * <desc>
     *      查询项目收费模式
     * </desc>
     *
     * @param projectId
     * @return
     * @author Jiaqi.X
     * @createDate 2019/10/23
     */
    public Integer searchProjectChargeModel(String projectId){
        return iPropertyEquipDao.searchProjectChargeModel(projectId);
    }
}